FUNCTION YBC_SWS_SEND_NEXT.
*"----------------------------------------------------------------------
*"*"本地接口：
*"  IMPORTING
*"     VALUE(IV_REQUESTID) TYPE  YBC_SWS_REQUEST-REQUESTID
*"  EXPORTING
*"     VALUE(ES_MSG) TYPE  BAPIRET2
*"  TABLES
*"      IT_APPROVER STRUCTURE  YBCS_SWS_APPROVER
*"----------------------------------------------------------------------
  DATA:
    LS_CREATOR  TYPE YBC_SWS_USER,
    LS_USER     TYPE YBC_SWS_USER,
    LT_USER     TYPE STANDARD TABLE OF YBC_SWS_USER,
    LS_REQUEST  TYPE YBC_SWS_REQUEST,
    LS_APP_CNFG TYPE YBC_SWS_APP_CNFG,
    LS_APPROVER TYPE YBC_SWS_APPROVER,
    LT_APPROVER TYPE STANDARD TABLE OF YBC_SWS_APPROVER.

  IF IT_APPROVER[] IS INITIAL.
    ES_MSG-TYPE = 'E'.
    MESSAGE S011(YBC_SWS) INTO ES_MSG-MESSAGE.
    RETURN.
  ENDIF.

  SELECT SINGLE *
    INTO LS_REQUEST
    FROM YBC_SWS_REQUEST
   WHERE REQUESTID = IV_REQUESTID.
  IF SY-SUBRC <> 0.
    ES_MSG-TYPE = 'E'.
    MESSAGE S010(YBC_SWS) INTO ES_MSG-MESSAGE WITH IV_REQUESTID.
    RETURN.
  ENDIF.

  SELECT SINGLE
    *
    INTO LS_APP_CNFG
    FROM YBC_SWS_APP_CNFG
   WHERE APPID = LS_REQUEST-APPID.

  CLEAR LS_CREATOR.
  SELECT SINGLE
    *
    INTO LS_CREATOR
    FROM YBC_SWS_USER
   WHERE USERID = LS_REQUEST-USERID.

  LOOP AT IT_APPROVER.
    TRANSLATE IT_APPROVER-EXTUSID TO UPPER CASE.
    TRANSLATE IT_APPROVER-SAPID TO UPPER CASE.
    TRANSLATE IT_APPROVER-EMAIL TO UPPER CASE.
    " TRANSLATE IT_APPROVER-TEL TO UPPER CASE.
    MODIFY IT_APPROVER.
  ENDLOOP.

  "审批者
  SELECT *
    INTO TABLE LT_USER
    FROM YBC_SWS_USER
    FOR ALL ENTRIES IN IT_APPROVER
   WHERE EXTUSID = IT_APPROVER-EXTUSID
     OR SAPID = IT_APPROVER-SAPID.
*     OR EMAIL = IT_APPROVER-EMAIL
*     OR TEL = IT_APPROVER-TEL.

  "检查审批者是否存在，不存在则创建
  LOOP AT IT_APPROVER.
    CLEAR LS_USER.

    IF IT_APPROVER-EXTUSID IS NOT INITIAL.
      READ TABLE LT_USER INTO LS_USER
        WITH KEY EXTUSID = IT_APPROVER-EXTUSID.
    ENDIF.

    IF LS_USER IS INITIAL.
      IF IT_APPROVER-SAPID IS NOT INITIAL.
        READ TABLE LT_USER INTO LS_USER
          WITH KEY SAPID = IT_APPROVER-SAPID.
      ENDIF.
    ENDIF.

*    IF LS_USER IS INITIAL.
*      IF IT_APPROVER-EMAIL IS NOT INITIAL.
*        READ TABLE LT_USER INTO LS_USER
*            WITH KEY EMAIL = IT_APPROVER-EMAIL.
*      ENDIF.
*    ENDIF.
*
*    IF LS_USER IS INITIAL.
*      IF IT_APPROVER-TEL IS NOT INITIAL.
*        READ TABLE LT_USER INTO LS_USER
*            WITH KEY EMAIL = IT_APPROVER-TEL.
*      ENDIF.
*    ENDIF.

    IF LS_USER IS INITIAL.
      MOVE-CORRESPONDING IT_APPROVER TO LS_USER.
      PERFORM FRM_CREATE_USER CHANGING LS_USER ES_MSG.
      IF ES_MSG-TYPE = 'E'.
        EXIT.
      ENDIF.
      APPEND LS_USER TO LT_USER.
    ENDIF.

    CLEAR LS_APPROVER.
    LS_APPROVER-REQUESTID = IV_REQUESTID.
    LS_APPROVER-APPROVERID = LS_USER-USERID.
    LS_APPROVER-APPLEVEL = IT_APPROVER-APPLEVEL.
    LS_APPROVER-APPLEVEL_DESC1 = IT_APPROVER-APPLEVEL_DESC1.
    LS_APPROVER-APPLEVEL_DESC2 = IT_APPROVER-APPLEVEL_DESC2.
    LS_APPROVER-APPLEVEL_DESC3 = IT_APPROVER-APPLEVEL_DESC3.
    LS_APPROVER-VERSION = IT_APPROVER-VERSION.
    LS_APPROVER-COUNTERSIGN = IT_APPROVER-COUNTERSIGN.
    LS_APPROVER-RDATE = SY-DATUM.
    LS_APPROVER-RTIME = SY-UZEIT.
    APPEND LS_APPROVER TO LT_APPROVER.
  ENDLOOP.

  MODIFY YBC_SWS_APPROVER FROM TABLE LT_APPROVER.
  IF SY-SUBRC = 0.
    COMMIT WORK AND WAIT.
    ES_MSG-TYPE = 'S'.
    MESSAGE S008(YBC_SWS) INTO ES_MSG-MESSAGE.
  ENDIF.

  "发送邮件
  DATA:
    LS_MAIL       TYPE YBCS_SWS_MAIL,
    LS_MAIL_MSG   TYPE BAPIRET2,
    LV_MAIL_ERROR TYPE C.

  IF LS_APP_CNFG-FLG_SEND_MAIL IS NOT INITIAL.
    LOOP AT LT_USER INTO LS_USER WHERE EMAIL IS NOT INITIAL .
      CLEAR: LS_MAIL, LS_MAIL_MSG, LV_MAIL_ERROR.
      "LS_USER 即为审批者
      PERFORM FORM_PREPARE_MAIL_DATA
        USING LS_APP_CNFG-MAIL_LANGU LS_CREATOR LS_USER LS_REQUEST
        CHANGING LS_MAIL LV_MAIL_ERROR.
      IF LV_MAIL_ERROR IS NOT INITIAL.
        CONTINUE.
      ENDIF.

      CALL FUNCTION 'YBC_SWS_SEND_MAIL'
        EXPORTING
          IS_MAIL = LS_MAIL
        IMPORTING
          ES_MSG  = LS_MAIL_MSG.
    ENDLOOP.
  ENDIF.
ENDFUNCTION.
